package org.example.daojishi;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class HolidayDaysCalculator {
    private static int calculateDaysToHoliday(Calendar date, int month, int day) {
        Calendar holidayDate = (Calendar) date.clone();
        holidayDate.set(Calendar.MONTH, month - 1);
        holidayDate.set(Calendar.DAY_OF_MONTH, day);

        if (holidayDate.before(date)) {
            holidayDate.add(Calendar.YEAR, 1);
        }

        int days = 0;
        while (date.before(holidayDate)) {
            if (!isWeekend(date)) { // 排除周末
                days++;
            }
            date.add(Calendar.DAY_OF_MONTH, 1);
        }

        return days - 1; // 不包括今天这一天
    }

    private static boolean isWeekend(Calendar date) {
        int dayOfWeek = date.get(Calendar.DAY_OF_WEEK);
        return dayOfWeek == Calendar.SATURDAY || dayOfWeek == Calendar.SUNDAY;
    }

    private static Holiday[] getHolidays(Calendar today) {
        Holiday[] holidays = {
                new Holiday("元旦", Calendar.JANUARY, 1, 1),
                new Holiday("春节", Calendar.FEBRUARY, 12, 7),
                new Holiday("清明节", Calendar.APRIL, 5, 3),
                new Holiday("劳动节", Calendar.MAY, 1, 3),
                new Holiday("端午节", Calendar.JUNE, 2, 3),
                new Holiday("中秋节", Calendar.SEPTEMBER, 10, 3),
                new Holiday("国庆节", Calendar.OCTOBER, 1, 7)
        };

        for (Holiday holiday : holidays) {
            int daysToHoliday = calculateDaysToHoliday(today, holiday.getMonth(), holiday.getDay());
            holiday.setDaysToHoliday(daysToHoliday);
        }

        return holidays;
    }

    public static void main(String[] args) {
        // 获取今天的日期
        Calendar today = Calendar.getInstance();

        // 设置输入日期的格式为"YYYY-MM-dd"
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        try {
            // 读取用户输入的日期
            String inputDateStr = "2023-12-27"; // 示例，可以替换为您需要的日期字符串
            today.setTime(sdf.parse(inputDateStr));
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }

        // 计算一年内的所有节日和工作日
        Holiday[] holidays = getHolidays(today);

        // 输出结果
        System.out.println("距离今天一年内的所有节日和周末:");
        for (Holiday holiday : holidays) {
            if (holiday.getDaysToHoliday() >= 0) {
                System.out.printf("%s: 还有 %d 天, 放假 %d 天\n",
                        holiday.getName(), holiday.getDaysToHoliday(), holiday.getVacationDays());
            }
        }
    }
}